package com.javier.nfcpass;

import java.util.Arrays;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.NfcA;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Tab1Fragment extends Fragment {

	private TextView lblInfoNFCOn;
	private NfcAdapter mNfcAdapter;
	private PendingIntent mPendingIntent;
	private IntentFilter[] mFilters;
	private String[][] mTechLists;

	private TextView txtUser, txtPass, txtURL, txtNombre, txtidTag,txtnfc;
	private BroadcastReceiver myReceiver = null;
		
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		final View fragmentView = inflater.inflate(R.layout.fragment1, container, false);
		final Button button = (Button) fragmentView.findViewById(R.id.btncrearSitio);
		capturarObjetos(fragmentView);
		
		IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
		myReceiver = new MyBroadcastReceiver(txtnfc);
		getActivity().registerReceiver( myReceiver, filter);
		
		

		lblInfoNFCOn = (TextView) fragmentView.findViewById(R.id.txtnfcT1);	
		button.setOnClickListener(onClickHandler);

		mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());

		if (mNfcAdapter != null && mNfcAdapter.isEnabled()) {
			lblInfoNFCOn.setText("Esperando NFC tag");
		} else {
			lblInfoNFCOn.setText("Servicio NFC desactivado");
		}

		// Create a generic PendingIntent that will be deliver to this activity.
		// The NFC stack
		// will fill in the intent with the details of the discovered tag before
		// delivering to
		// this activity.
		mPendingIntent = PendingIntent.getActivity(getActivity(), 0,
				new Intent(getActivity(), getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

		// set an intent filter for all MIME data
		IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
		// try {
		// ndefIntent.addDataType("*/*");
		// mIntentFilters = new IntentFilter[] { ndefIntent };
		// } catch (Exception e) {
		// Log.e("TagDispatch", e.toString());
		// }
		mFilters = new IntentFilter[] { ndef };
		mTechLists = new String[][] { new String[] { MifareClassic.class.getName(), NfcA.class.getName() } };

		lblInfoNFCOn.setOnClickListener(new OnClickListener() {

	        @Override
	        public void onClick(View v) {
	             // Send the user to the settings page and hope they turn it on
                if (android.os.Build.VERSION.SDK_INT >= 16)
                {
                    startActivity(new Intent(android.provider.Settings.ACTION_NFC_SETTINGS));
                }
                else
                {
                    startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                }
	        }
		});
	        
		return fragmentView;

	}

    @Override
    public void onResume() {
        super.onResume();
        myReceiver = new MyBroadcastReceiver(txtnfc);
        getActivity().registerReceiver( myReceiver, new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED));
        if (mNfcAdapter != null) 
        	Log.e("onResume", "enableForegroundDispatch");
        	mNfcAdapter.enableForegroundDispatch(getActivity(), mPendingIntent, mFilters,
                mTechLists);
    }
    
    @Override
    public void onPause() {
        super.onPause();
        getActivity().unregisterReceiver(myReceiver);
        if (mNfcAdapter != null) 
        	Log.e("onPause", "disableForegroundDispatch");
        	mNfcAdapter.disableForegroundDispatch(getActivity());
    }
    /*
    @Override
    public void onDestroy() {
        super.onDestroy();

        // Remove the broadcast listener
        getActivity().unregisterReceiver(myReceiver);
    }
    */
	private OnClickListener onClickHandler = new OnClickListener() {

		@Override
		public void onClick(View v) {

			GestorBD BD;

			BD = new GestorBD(getView().getContext());
			BD.crearSitio(txtNombre.getText().toString(), txtUser.getText().toString(), txtPass.getText().toString(),
					txtURL.getText().toString(), txtidTag.getText().toString());

			limpiarDatos();

		}

	};

	public void actualizarNFC(String idnfc) {
		//TextView txtnfc, txtidTag;
		//txtnfc = (TextView) getView().findViewById(R.id.txtnfcT1);
		//txtidTag = (TextView) getView().findViewById(R.id.txtidTagT1);
		txtnfc.setText(idnfc);
		txtidTag.setText(idnfc);
	}

	public void limpiarDatos() {

		try {

			txtUser.setText("");
			txtNombre.setText("");
			txtURL.setText("");
			txtPass.setText("");
			txtidTag.setText("");

		} catch (Exception e) {
			Log.e("limpiarDatos", e.toString());
		}
	}

	private void capturarObjetos(View vista) {
		txtUser = (TextView) vista.findViewById(R.id.txtUsuarioT1);
		txtNombre = (TextView) vista.findViewById(R.id.txtNombreT1);
		txtURL = (TextView) vista.findViewById(R.id.txtURLT1);
		txtPass = (TextView) vista.findViewById(R.id.txtPassT1);
		txtidTag = (TextView) vista.findViewById(R.id.txtidTagT1);
		txtnfc = (TextView) vista.findViewById(R.id.txtnfcT1);
	}

}


